package com.tybbank.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.IntoBean;
import com.tybbank.Bean.YZMBean;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.ActivityUtil;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.StatusBarUtil;
import com.tybbank.Utils.logUtilssss;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/11/27 0027.
 */

public class TiSuActivity extends BaseActivity {
    @BindView(R.id.ib_exit)
    ImageButton ibExit;
    @BindView(R.id.tv_tisu)
    TextView tvTisu;
    @BindView(R.id.line_tisu)
    View lineTisu;
    @BindView(R.id.rl_tisu)
    RelativeLayout rlTisu;
    @BindView(R.id.tv_dingqi)
    TextView tvDingqi;
    @BindView(R.id.line_dingqi)
    View lineDingqi;
    @BindView(R.id.rl_dingqi)
    RelativeLayout rlDingqi;
    @BindView(R.id.iv_image)
    ImageButton ivImage;
    @BindView(R.id.tv_keyongzhi)
    TextView tvKeyongzhi;
    @BindView(R.id.tv_tisuzhi)
    TextView tvTisuzhi;
    @BindView(R.id.tv_keyongzhi_num)
    TextView tvKeyongzhiNum;
    @BindView(R.id.et_keyongnumber)
    EditText etKeyongnumber;
    @BindView(R.id.tv_shifang_num)
    TextView tvShifangNum;
    @BindView(R.id.et_pay_pwdnum)
    EditText etPayPwdnum;
    @BindView(R.id.tv_beizhu)
    TextView tvBeizhu;
    @BindView(R.id.bt_enter)
    Button btEnter;
    @BindView(R.id.tv_num_type)
    TextView tvNumType;
    @BindView(R.id.rl_gone)
    RelativeLayout rlGone;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.tv_jilu)
    TextView tvJilu;
    @BindView(R.id.tv_keyong)
    TextView tvKeyong;
    @BindView(R.id.textView)
    TextView textView;
    private int tag = 1;
    private int tagtwo = 1;
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tisu);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            StatusBarUtil.setStatusBarColor(TiSuActivity.this, R.color.backgroundcolorhei723);
        }
        if (Build.VERSION.SDK_INT >= 21) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }

        /*-------------------------- 中间这一段只显示定期 把提速隐藏了  需要回复 直接 删除中间一段就可以---  137行记得 打开 布局自己改------------------------------------------*/
        rlTisu.setVisibility(View.GONE);
        tvKeyong.setText(getString(R.string.keyong));
        ivImage.setEnabled(false);
        tvKeyongzhiNum.setText(getIntent().getStringExtra("abc_coin"));
        tvTisuzhi.setText(getString(R.string.dingqi));
        tvKeyongzhi.setText(getString(R.string.keyong));
        tvJilu.setText(getString(R.string.jilu));
        //  tvJilu.setText("定期记录");
        ActivityUtil.beizhu(TiSuActivity.this, 2, tvBeizhu);//  购买说明
        tag = 2;
        tvDingqi.setTextColor(getResources().getColor(R.color.textcolorbai));
        lineDingqi.setBackgroundColor(getResources().getColor(R.color.textcolormain));
        tvTisu.setTextColor(getResources().getColor(R.color.textcolorbai));
        lineTisu.setBackgroundColor(getResources().getColor(R.color.textcolorbai));
        rlGone.setVisibility(View.GONE);
        ivImage.setImageDrawable(getResources().getDrawable(R.mipmap.icon_regular));

        /*-----------------------------------------------------------------------*/


        tvJilu.setText(getString(R.string.jilu));
        String abc_coin = getIntent().getStringExtra("abc_coin");
        tvKeyongzhiNum.setText(abc_coin);
    ///    ActivityUtil.beizhu(TiSuActivity.this, 1, tvBeizhu);//  购买说明
        etKeyongnumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!TextUtils.isEmpty(etKeyongnumber.getText().toString().trim())) {
                    double aDouble = Double.parseDouble(etKeyongnumber.getText().toString().trim());
                    double bz = Double.parseDouble(MyApplication.lllllllllll);
                    tvShifangNum.setText((aDouble * bz) / 100 + getString(R.string.num_day));
                } else {
                    tvShifangNum.setText("0" + getString(R.string.num_day));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick({R.id.tv_jilu, R.id.iv_image, R.id.rl_tisu, R.id.rl_dingqi, R.id.bt_enter, R.id.ib_exit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_jilu:      //记录
                if (tag == 1) {
                    if (tagtwo == 1) {
                        intent = new Intent(this, RecordActivity.class);
                        intent.putExtra("type", "2");
                        intent.putExtra("classify", "2");
                        intent.putExtra("title", getString(R.string.keyong_tisu));
                        startActivity(intent);
                    } else {
                        intent = new Intent(this, RecordActivity.class);
                        intent.putExtra("type", "3");
                        intent.putExtra("classify", "2");
                        intent.putExtra("title", getString(R.string.tisu_keyong));
                        startActivity(intent);
                    }
                } else if (tag == 2) {
                    intent = new Intent(this, RecordActivity.class);
                    intent.putExtra("type", "3");
                    intent.putExtra("classify", "3");
                    intent.putExtra("title", getString(R.string.keyong_dingqi));
                    startActivity(intent);

                }
                break;

            case R.id.ib_exit:
                TiSuActivity.this.finish();
                break;
            case R.id.iv_image: // 来回切换的标致
                if (tag == 1) {
                    tvJilu.setText(getString(R.string.jilu));
                    if (tagtwo == 1) {
                        tvKeyongzhiNum.setText(getIntent().getStringExtra("current"));
                        tagtwo = 2;
                        //  tvJilu.setText("可用记录");
                        tvJilu.setText(getString(R.string.jilu));
                        rlGone.setVisibility(View.GONE);
                        tvTisuzhi.setText(getString(R.string.keyong));
                        tvKeyongzhi.setText(getString(R.string.tisuzhi));
                        tvKeyong.setText(getString(R.string.tisuzhi));
                        ivImage.setImageDrawable(getResources().getDrawable(R.mipmap.icon_speed));
                    } else {
                        tvKeyongzhiNum.setText(getIntent().getStringExtra("abc_coin"));
                        //  tvJilu.setText("提速记录");
                        tvJilu.setText(getString(R.string.jilu));
                        tagtwo = 1;
                        tvTisuzhi.setText(getString(R.string.tisuzhi));
                        tvKeyong.setText(getString(R.string.keyong));
                        tvKeyongzhi.setText(getString(R.string.keyong));
                        rlGone.setVisibility(View.VISIBLE);
                        ivImage.setImageDrawable(getResources().getDrawable(R.mipmap.icon_speed));
                    }
                } else {
                    String abc_coin = getIntent().getStringExtra("abc_coin");
                    tvKeyongzhiNum.setText(abc_coin);
                    //  tvJilu.setText("定期记录");
                    tvJilu.setText(getString(R.string.jilu));
                    tagtwo = 1;
                    tvTisuzhi.setText(getString(R.string.dingqi));
                    tvKeyongzhi.setText(getString(R.string.keyong));
                    rlGone.setVisibility(View.GONE);
                    ivImage.setImageDrawable(getResources().getDrawable(R.mipmap.icon_regular));
                }
                break;
            case R.id.rl_tisu:  //提速按钮
                ivImage.setEnabled(true);
                tvKeyong.setText(getString(R.string.keyong));
                tvKeyongzhiNum.setText(getIntent().getStringExtra("abc_coin"));
                tvTisuzhi.setText(getString(R.string.tisuzhi));
                tvKeyongzhi.setText(getString(R.string.keyong));
                //   tvJilu.setText("提速记录");
                tvJilu.setText(getString(R.string.jilu));
                ActivityUtil.beizhu(TiSuActivity.this, 1, tvBeizhu);
                tag = 1;
                tvTisu.setTextColor(getResources().getColor(R.color.textcolormain));
                lineTisu.setBackgroundColor(getResources().getColor(R.color.textcolormain));
                tvDingqi.setTextColor(getResources().getColor(R.color.textcolorbai));
                lineDingqi.setBackgroundColor(getResources().getColor(R.color.textcolorbai));
                rlGone.setVisibility(View.VISIBLE);
                ivImage.setImageDrawable(getResources().getDrawable(R.mipmap.icon_speed));
                break;
            case R.id.rl_dingqi:    // 定期按钮
                tvKeyong.setText(getString(R.string.keyong));
                ivImage.setEnabled(false);
                tvKeyongzhiNum.setText(getIntent().getStringExtra("abc_coin"));
                tvTisuzhi.setText(getString(R.string.dingqi));
                tvKeyongzhi.setText(getString(R.string.keyong));
                tvJilu.setText(getString(R.string.jilu));
                //  tvJilu.setText("定期记录");
                ActivityUtil.beizhu(TiSuActivity.this, 2, tvBeizhu);//  购买说明
                tag = 2;
                tvDingqi.setTextColor(getResources().getColor(R.color.textcolormain));
                lineDingqi.setBackgroundColor(getResources().getColor(R.color.textcolormain));
                tvTisu.setTextColor(getResources().getColor(R.color.textcolorbai));
                lineTisu.setBackgroundColor(getResources().getColor(R.color.textcolorbai));
                rlGone.setVisibility(View.GONE);
                ivImage.setImageDrawable(getResources().getDrawable(R.mipmap.icon_regular));
                break;
            case R.id.bt_enter:
                if (TextUtils.isEmpty(etKeyongnumber.getText().toString().trim())) {
                    logUtilssss.toast(this, getString(R.string.hintnum));
                    return;
                } else if (TextUtils.isEmpty(etPayPwdnum.getText().toString().trim())) {
                    logUtilssss.toast(this, getString(R.string.paypwdhint));
                    return;
                } else {
                    if (tag == 1) {
                        if (tagtwo == 1) {
                            initview("1"); //	1 可用转入活期 2 活期转入可用
                        } else {
                            initview("2");//	1 可用转入活期 2 活期转入可用
                        }
                    } else if (tag == 2) {
                        initw();

                    }

                }
                break;
        }
    }



    private void initview(String type) {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("pay_pwd", MD5Utils.md5Password(etPayPwdnum.getText().toString().trim()));
        parameters.put("number", etKeyongnumber.getText().toString().trim());
        parameters.put("type", type);
        parameters.put("token", MyApplication.token);
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, "正在加载...");
        dialog.show();
        OkHttpUtils.post()
                .url(Constant.interturn)
                .addParams("uuid", MyApplication.uuid)
                .addParams("type", type)
                .addParams("token", MyApplication.token)
                .addParams("pay_pwd", MD5Utils.md5Password(etPayPwdnum.getText().toString().trim()))
                .addParams("TimeStamp", time)
                .addParams("number", etKeyongnumber.getText().toString().trim())
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG可用和提速互转" + response);
                        dialog.dismiss();
                        try {
                            YZMBean result = new Gson().fromJson(response, YZMBean.class);
                            if (result.getCode() == 1011) {
                                showTips(result.getMsg());
                                setResult(RESULT_OK);
                                TiSuActivity.this.finish();
                            } else if (result.getCode() == 1004) {
                                Intent intent = new Intent(TiSuActivity.this, LoginActivity.class);
                                startActivity(intent);
                                return;
                            } else {
                                showTips(result.getMsg());
                            }
                        } catch (Exception e) {
                        }
                    }
                });
    }

    private void initw() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("pay_pwd", MD5Utils.md5Password(etPayPwdnum.getText().toString().trim()));
        parameters.put("number", etKeyongnumber.getText().toString().trim());
        parameters.put("token", MyApplication.token);
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, "正在加载...");
        dialog.show();
        OkHttpUtils.post()
                .url(Constant.increase_regular)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("pay_pwd", MD5Utils.md5Password(etPayPwdnum.getText().toString().trim()))
                .addParams("TimeStamp", time)
                .addParams("number", etKeyongnumber.getText().toString().trim())
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String json, int id) {
                        logUtilssss.e("TAG可用转入定期" + json);
                        dialog.dismiss();
                        try {
                            IntoBean result = new Gson().fromJson(json, new TypeToken<IntoBean>() {
                            }.getType());
                            if (result.getCode() == 1011) {
                                showTips(result.getMsg());
                                setResult(RESULT_OK);
                                TiSuActivity.this.finish();
                            } else if (result.getCode() == 1004) {
                                Intent intent = new Intent(TiSuActivity.this, LoginActivity.class);
                                startActivity(intent);
                                return;
                            } else {
                                showTips(result.getMsg());
                            }
                        } catch (Exception e) {
                        }
                    }
                });
    }
}
